package it.unipi.di.rasip.utils;

import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class RasipLabel extends CustomItem {
	private Image icon;
	private String text;
	private Font font;
	private Color bgColor, textColor;
	private int hGap, vGap;
	
	private boolean visible;

	public RasipLabel(String text) {
		super(null);
		this.text = text;
		font =  Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
		textColor = Color.BLACK;
		hGap = vGap = 3;
	}

	protected int getMinContentHeight() {
		return 0;
	}

	protected int getMinContentWidth() {
		return 0;
	}

	protected int getPrefContentHeight(int arg0) {
		if(!this.isVisible()) return 0;
		
		if (icon != null) {
			return Math.max( Font.getDefaultFont().getHeight(), icon.getHeight()) + 2*vGap ;
		} else { 
			return Font.getDefaultFont().getHeight() + 2*vGap ;
		}
	}

	protected int getPrefContentWidth(int arg0) {
		if(!this.isVisible()) return 0;
		
		if (icon != null) {
			return hGap + icon.getWidth() + hGap + getFont().stringWidth(text) + hGap;
		} else {
			return hGap + getFont().stringWidth(text) + hGap;
		}
	}

	protected void paint(Graphics g, int w, int h) {
		if(!this.isVisible()) return;
		
		int x = hGap;
		if (bgColor != null) {
			g.setColor(bgColor.getCode());
			g.fillRoundRect(0,0,w,h,10,10);
		}
	    g.setFont(getFont());
	    if (icon != null) {
	    	g.drawImage (icon, x, vGap, 0);
	    	x += icon.getWidth() + hGap; 
	    }

	    g.setColor(textColor.getCode());
		g.drawString(text, x, vGap, 0);
	}

	public Font getFont() {
		return font;
	}

	public void setFont(Font font) {
		this.font = font;
	}

	public Color getBgColor() {
		return bgColor;
	}

	public void setBgColor(Color bgColor) {
		this.bgColor = bgColor;
	}

	public Color getTextColor() {
		return textColor;
	}

	public void setTextColor(Color textColor) {
		this.textColor = textColor;
	}

	public Image getIcon() {
		return icon;
	}

	public void setIcon(Image icon) {
		this.icon = icon;
	}

	public int getHGap() {
		return hGap;
	}

	public void setHGap(int gap) {
		hGap = gap;
	}

	public int getVGap() {
		return vGap;
	}

	public void setVGap(int gap) {
		vGap = gap;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
		this.invalidate();
	}

}
